package com.xykj.enterprise.wechat.ext.api.schedule;

import com.xykj.enterprise.wechat.bean.ext.schedule.AddScheduleVo;
import com.xykj.enterprise.wechat.ext.service.schedule.ScheduleService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;

@Slf4j
@Function(description = "创建日程", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "schedule", type = Type.String, description = "日程信息(json格式)", required = true),
        @Parameter(name = "agentid", type = Type.String, description = "授权方安装的应用agentid。仅旧的第三方多应用套件需要填此参数", required = false),
})
@Component
public class AddSchedule implements Action {

    @Autowired
    private ScheduleService scheduleService;

    @Override
    public Response execute(Request request) throws Exception {

        String schedule = request.getString("schedule");

        String json = URLDecoder.decode(URLDecoder.decode(schedule, "UTF-8"));
        log.debug("scheduleInfo:{}", json);

        AddScheduleVo rsp = scheduleService.addSchedule(
                request.getString("access_token"),
                json
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
